class WeatherModel {
  final String city;
  final String weather;
  final String description;
  final double temperature;
  final double feelsLike;
  final int humidity;
  final double windSpeed;
  final String windDirection;
  final int pressure;
  final int visibility;
  final String icon;
  final DateTime date;

  WeatherModel({
    required this.city,
    required this.weather,
    required this.description,
    required this.temperature,
    required this.feelsLike,
    required this.humidity,
    required this.windSpeed,
    required this.windDirection,
    required this.pressure,
    required this.visibility,
    required this.icon,
    required this.date,
  });

  factory WeatherModel.fromJson(Map<String, dynamic> json) {
    return WeatherModel(
      city: json['name'] ?? '',
      weather: json['weather'][0]['main'] ?? '',
      description: json['weather'][0]['description'] ?? '',
      temperature: (json['main']['temp'] as num).toDouble(),
      feelsLike: (json['main']['feels_like'] as num).toDouble(),
      humidity: json['main']['humidity'] ?? 0,
      windSpeed: (json['wind']['speed'] as num).toDouble(),
      windDirection: _getWindDirection(json['wind']['deg'] ?? 0),
      pressure: json['main']['pressure'] ?? 0,
      visibility: json['visibility'] ?? 0,
      icon: json['weather'][0]['icon'] ?? '',
      date: DateTime.now(),
    );
  }

  static String _getWindDirection(int degrees) {
    if (degrees >= 337.5 || degrees < 22.5) return '北';
    if (degrees >= 22.5 && degrees < 67.5) return '东北';
    if (degrees >= 67.5 && degrees < 112.5) return '东';
    if (degrees >= 112.5 && degrees < 157.5) return '东南';
    if (degrees >= 157.5 && degrees < 202.5) return '南';
    if (degrees >= 202.5 && degrees < 247.5) return '西南';
    if (degrees >= 247.5 && degrees < 292.5) return '西';
    if (degrees >= 292.5 && degrees < 337.5) return '西北';
    return '北';
  }
}

class DailyForecast {
  final DateTime date;
  final String weather;
  final String icon;
  final double maxTemp;
  final double minTemp;
  final int humidity;
  final double windSpeed;

  DailyForecast({
    required this.date,
    required this.weather,
    required this.icon,
    required this.maxTemp,
    required this.minTemp,
    required this.humidity,
    required this.windSpeed,
  });

  factory DailyForecast.fromJson(Map<String, dynamic> json) {
    return DailyForecast(
      date: DateTime.fromMillisecondsSinceEpoch(json['dt'] * 1000),
      weather: json['weather'][0]['main'] ?? '',
      icon: json['weather'][0]['icon'] ?? '',
      maxTemp: (json['temp']['max'] as num).toDouble(),
      minTemp: (json['temp']['min'] as num).toDouble(),
      humidity: json['humidity'] ?? 0,
      windSpeed: (json['wind_speed'] as num).toDouble(),
    );
  }
}

class AirQuality {
  final int aqi;
  final String level;
  final String description;

  AirQuality({
    required this.aqi,
    required this.level,
    required this.description,
  });

  factory AirQuality.fromJson(Map<String, dynamic> json) {
    final aqi = json['list'][0]['main']['aqi'] ?? 1;
    String level;
    String description;

    switch (aqi) {
      case 1:
        level = '优';
        description = '空气质量良好，适合户外活动';
        break;
      case 2:
        level = '良';
        description = '空气质量可接受，敏感人群应减少户外活动';
        break;
      case 3:
        level = '轻度污染';
        description = '敏感人群应减少户外活动';
        break;
      case 4:
        level = '中度污染';
        description = '应减少户外活动';
        break;
      case 5:
        level = '重度污染';
        description = '避免户外活动';
        break;
      default:
        level = '未知';
        description = '暂无数据';
    }

    return AirQuality(
      aqi: aqi,
      level: level,
      description: description,
    );
  }
}

class LifeAdvice {
  final String clothing;
  final String uv;
  final String exercise;
  final String travel;

  LifeAdvice({
    required this.clothing,
    required this.uv,
    required this.exercise,
    required this.travel,
  });

  factory LifeAdvice.fromWeather(WeatherModel weather) {
    String clothing;
    String uv;
    String exercise;
    String travel;

    // 穿衣建议
    if (weather.temperature >= 30) {
      clothing = '天气炎热，建议穿轻薄透气的衣物';
    } else if (weather.temperature >= 20) {
      clothing = '天气适宜，建议穿薄外套或长袖衬衫';
    } else if (weather.temperature >= 10) {
      clothing = '天气较凉，建议穿厚外套或毛衣';
    } else {
      clothing = '天气寒冷，建议穿厚羽绒服或棉衣';
    }

    // 紫外线建议
    if (weather.weather.contains('晴')) {
      uv = '紫外线强，建议涂抹防晒霜，戴遮阳帽';
    } else if (weather.weather.contains('多云')) {
      uv = '紫外线中等，建议适当防晒';
    } else {
      uv = '紫外线较弱，无需特别防护';
    }

    // 运动建议
    if (weather.weather.contains('雨') || weather.weather.contains('雪')) {
      exercise = '天气不适合户外运动，建议室内运动';
    } else if (weather.windSpeed > 10) {
      exercise = '风力较大，建议减少户外运动';
    } else {
      exercise = '天气适宜户外运动';
    }

    // 出行建议
    if (weather.weather.contains('雨')) {
      travel = '有雨，出门请带伞';
    } else if (weather.weather.contains('雪')) {
      travel = '有雪，注意路面湿滑';
    } else if (weather.windSpeed > 15) {
      travel = '风力较大，注意安全';
    } else {
      travel = '天气适宜出行';
    }

    return LifeAdvice(
      clothing: clothing,
      uv: uv,
      exercise: exercise,
      travel: travel,
    );
  }
}

class CaiyunRealtime {
  final String skycon;
  final double temperature;
  final int humidity;
  final double windSpeed;
  final int aqi;
  final String aqiDescription;

  CaiyunRealtime({
    required this.skycon,
    required this.temperature,
    required this.humidity,
    required this.windSpeed,
    required this.aqi,
    required this.aqiDescription,
  });

  factory CaiyunRealtime.fromJson(Map<String, dynamic> json) {
    final air = json['air_quality'] ?? {};
    final aqi = air['aqi']?['chn'] ?? 0;
    String aqiDesc = '优';
    if (aqi >= 0 && aqi <= 50) {
      aqiDesc = '优';
    } else if (aqi <= 100) {
      aqiDesc = '良';
    } else if (aqi <= 150) {
      aqiDesc = '轻度污染';
    } else if (aqi <= 200) {
      aqiDesc = '中度污染';
    } else if (aqi <= 300) {
      aqiDesc = '重度污染';
    } else {
      aqiDesc = '严重污染';
    }
    return CaiyunRealtime(
      skycon: json['skycon'] ?? '',
      temperature: (json['temperature'] as num).toDouble(),
      humidity: ((json['humidity'] as num) * 100).round(),
      windSpeed: (json['wind']?['speed'] as num?)?.toDouble() ?? 0.0,
      aqi: aqi,
      aqiDescription: aqiDesc,
    );
  }
}

class CaiyunDailyForecast {
  final DateTime date;
  final String skycon;
  final double maxTemp;
  final double minTemp;
  final int aqi;

  CaiyunDailyForecast({
    required this.date,
    required this.skycon,
    required this.maxTemp,
    required this.minTemp,
    required this.aqi,
  });

  factory CaiyunDailyForecast.fromJson(Map<String, dynamic> json, int index) {
    final List<dynamic>? airQualityList = json['air_quality']?['aqi'];
    Map<String, dynamic>? dailyAqiData;

    if (airQualityList != null && airQualityList.length > index) {
      dailyAqiData = airQualityList[index] as Map<String, dynamic>?;
    }

    final int aqiValue = (dailyAqiData?['avg']?['chn'] as num?)?.toInt() ?? 0;

    return CaiyunDailyForecast(
      date: DateTime.parse(json['skycon'][index]['date']),
      skycon: json['skycon'][index]['value'],
      maxTemp: (json['temperature'][index]['max'] as num).toDouble(),
      minTemp: (json['temperature'][index]['min'] as num).toDouble(),
      aqi: aqiValue,
    );
  }
}

class CaiyunLifeIndex {
  final String coldRisk;
  final String dressing;
  final String ultraviolet;
  final String carWashing;

  CaiyunLifeIndex({
    required this.coldRisk,
    required this.dressing,
    required this.ultraviolet,
    required this.carWashing,
  });

  factory CaiyunLifeIndex.fromJson(Map<String, dynamic> json) {
    return CaiyunLifeIndex(
      coldRisk: json['coldRisk'][0]['desc'] ?? '',
      dressing: json['dressing'][0]['desc'] ?? '',
      ultraviolet: json['ultraviolet'][0]['desc'] ?? '',
      carWashing: json['carWashing'][0]['desc'] ?? '',
    );
  }
}

class WeatherAlert {
  final String type; // 如"高温"、"暴雨"等
  final String message;
  final DateTime time;

  WeatherAlert({
    required this.type,
    required this.message,
    required this.time,
  });
}

class CustomWeatherReminder {
  final String condition; // 如"温度高于30度"、"下雨"
  final double? threshold; // 阈值
  final String? weatherType; // 天气类型，如"雨"
  final bool enabled;

  CustomWeatherReminder({
    required this.condition,
    this.threshold,
    this.weatherType,
    this.enabled = true,
  });
}
